<?php
/**
 * 支付接口
 * 获取订单信息进行存入数据并进行异步回调
 * 验证用户身份token
 * @author Edwin
 */

namespace V1\Controller;

use Common\Controller\ApiController;
use Common\Common\AliPay;

class PayController extends ApiController
{
    public function alipay()
    {
        $log = M('pay');
        $alipay = new AliPay();
        $userid = $this->userId;
        $total_amount = number_format(I('post.money'), 2);
        //生成订单码
        $out_trade_no = $this->MyOrderNo22();
        $data['userid'] = $userid;
        $data['out_trade_no'] = $out_trade_no;
        $data['gold'] = C('GOLD_RATE') * $out_trade_no;
        $data['money'] = $total_amount;
        $data['add_ip'] = get_client_ip();
        $data['create_time'] = time();
        $data['remark'] = '支付宝支付';
        $data['paytype'] = !empty(I('post.paytype')) ? I('post.paytype') : "1"; //1为支付宝，2为微信
        $res = $log->add($data);
        if (!$res) {
            $this->myApiPrint(C('STATUS')['228'], 400);
        } else {
            //创建支付订单参数
            $order_info = $alipay->tradeAppPay($data['userid'], $data['out_trade_no'], $data['money'], $data['add_ip'], $data['create_time'], $data['remark'], $data['paytype']);
//将获取的参数返回给app端 jsonReturn是自定义的方法
            $this->myApiPrint('success', 200, $order_info);
        }
    }

    /**
     * 异步回调
     */
    public function notify_url()
    {
        $log = M('pay');
        $alipay = new AliPay();
        $verify_result = $alipay->rsaCheck($_POST, 'RSA');
        if ($verify_result) {//验证成功
            //获取支付宝的通知返回参数，可参考技术文档中服务器异步通知参数列表
            //商户订单号
            $out_trade_no = $_POST['out_trade_no'];
            //支付宝交易号
            $trade_no = $_POST['trade_no'];
            //交易状态
            $trade_status = $_POST['trade_status'];
            //交易时间
            $trade_time = $_POST['notify_time'];
//			logResult($trade_status);
            if ($trade_status == 'TRADE_FINISHED' || $trade_status == 'TRADE_SUCCESS') {
                $data['trade_no'] = $trade_no;
                $data['trade_time'] = $trade_time;
                $data['status'] = 1;
                $where['out_trade_no'] = $out_trade_no;;
                $order = $log->where($where)->find();
                if ($order['status'] == 1) {
                    echo "fail";
                } else {
                    $map['userid'] = $order['userid'];
                    $res = $log->where($where)->save($data);
                    $gold = D('MemberProfile')->where($map)->setInc('gold', $order['money']);
//                 updateOrder($out_trade_no); //更新订单函数
                    //向个人账户跟系统账户更改信息
//                    $result = $finance_model->addlog($userid,$log,$total_amount,1,1);
//                    if(!$result['status']){
//                        //支付成功但是修改失败，留记录
//                        error_asset_log($out_trade_no,$trade_no,$userid,$total_amount);
//                    }
                    if ($res === 'false' || $gold === 'false') {
                        echo "save fail";   //更新失败
                    } else {
                        echo "success";     //更新成功
//			            logResult("成功，商户订单号".$out_trade_no.",支付宝交易号：".$trade_no.",交易状态：".$trade_status);
                    }
                }
            } else {
                echo "fail";
            }
        } else {
            //验证失败
            echo "fail";
//            logResult("失败");
        }
    }
}
